MVVM (Model-View-ViewModel) প্যাটার্নে Validation এবং Error Notification অত্যন্ত গুরুত্বপূর্ণ ভূমিকা পালন করে, কারণ এগুলি ইউজারের ইনপুটের সঠিকতা নিশ্চিত করে এবং ভুল ইনপুটের জন্য ইউজারকে যথাযথভাবে জানান দেয়। এই প্রক্রিয়াগুলি Model এবং ViewModel এর মধ্যে কাজ করে এবং View-এ এর ফলাফল প্রদর্শিত হয়।
Validation হল একটি প্রক্রিয়া যার মাধ্যমে ইউজারের ইনপুট যাচাই করা হয়, যাতে নিশ্চিত হওয়া যায় যে ইনপুটটি সঠিক এবং বৈধ। MVVM প্যাটার্নে, ইনপুট ভ্যালিডেশন সাধারণত ViewModel বা Model এ করা হয়, এবং ফলাফল View-এ Error Notification আকারে প্রদর্শিত হয়।
Data Annotations:
Required
, StringLength
, Range
, RegularExpression
ইত্যাদি।Data Annotations উদাহরণ:
public class Product
{
[Required(ErrorMessage = "Product name is required.")]
public string Name { get; set; }
[Range(0, 10000, ErrorMessage = "Price must be between 0 and 10,000.")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Quantity is required.")]
[Range(1, 100, ErrorMessage = "Quantity must be between 1 and 100.")]
public int Quantity { get; set; }
}
IValidatableObject Interface:
Validate
প্রদান করে, যা প্রপার্টি ভ্যালিডেশনের সময় চেক করা হয় এবং যদি কোনো ত্রুটি থাকে, তাহলে সেই ত্রুটির বার্তা ফিরিয়ে দেয়।IValidatableObject উদাহরণ:
public class Product : IValidatableObject
{
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Price < 0)
{
yield return new ValidationResult("Price cannot be negative", new[] { "Price" });
}
if (Quantity < 1 || Quantity > 100)
{
yield return new ValidationResult("Quantity must be between 1 and 100", new[] { "Quantity" });
}
}
}
Custom Validation Methods:
Custom Validation উদাহরণ:
public class LoginViewModel : INotifyPropertyChanged
{
private string _password;
private string _confirmPassword;
public string Password
{
get => _password;
set
{
_password = value;
OnPropertyChanged();
ValidatePasswords();
}
}
public string ConfirmPassword
{
get => _confirmPassword;
set
{
_confirmPassword = value;
OnPropertyChanged();
ValidatePasswords();
}
}
private void ValidatePasswords()
{
if (Password != ConfirmPassword)
{
// Set error message
ErrorMessage = "Passwords do not match.";
}
else
{
ErrorMessage = null;
}
}
public string ErrorMessage { get; set; }
}
Error Notification হল সেই প্রক্রিয়া, যার মাধ্যমে ইউজারের ভুল ইনপুট বা ভ্যালিডেশন সমস্যা সম্পর্কে তাকে অবহিত করা হয়। MVVM প্যাটার্নে, এই ত্রুটির বার্তা সাধারণত ViewModel থেকে View-এ পাঠানো হয় এবং ইউজারকে দেখানো হয়।
INotifyDataErrorInfo Interface:
INotifyDataErrorInfo উদাহরণ:
public class ProductViewModel : INotifyDataErrorInfo
{
private readonly Dictionary<string, List<string>> _errors = new();
public string Name { get; set; }
public decimal Price { get; set; }
public bool HasErrors => _errors.Any();
public IEnumerable<string> GetErrors(string propertyName)
{
if (_errors.ContainsKey(propertyName))
return _errors[propertyName];
return Enumerable.Empty<string>();
}
public void Validate()
{
if (string.IsNullOrEmpty(Name))
{
_errors["Name"] = new List<string> { "Name is required." };
}
else
{
_errors.Remove("Name");
}
if (Price < 0)
{
_errors["Price"] = new List<string> { "Price must be non-negative." };
}
else
{
_errors.Remove("Price");
}
OnPropertyChanged(nameof(HasErrors));
}
}
ErrorTemplate (UI Level):
ErrorTemplate উদাহরণ:
<Window.Resources>
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderBrush="Red" BorderThickness="1">
<TextBlock Foreground="Red" Text="{Binding}" />
</Border>
</ControlTemplate>
</Window.Resources>
<TextBox Text="{Binding Name}" Width="200">
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<StackPanel>
<TextBox Name="TextBox" Text="{Binding Name}" />
<TextBlock Text="{Binding Path=ErrorMessage}" Foreground="Red"/>
</StackPanel>
</ControlTemplate>
</TextBox.Template>
</TextBox>
Validation এবং Error Notification MVVM প্যাটার্নে অ্যাপ্লিকেশনের ইউজার ইনপুট সঠিকতা নিশ্চিত করতে এবং ইউজারকে ত্রুটির সঠিক বার্তা প্রদর্শন করতে গুরুত্বপূর্ণ ভূমিকা পালন করে। Data Annotations, IValidatableObject, এবং INotifyDataErrorInfo ইন্টারফেসের মাধ্যমে আপনি আপনার অ্যাপ্লিকেশনে ইফেক্টিভ ভ্যালিডেশন ইমপ্লিমেন্ট করতে পারেন এবং Error Templates এবং Command এর মাধ্যমে ইউজারকে ত্রুটির বার্তা উপস্থাপন করতে পারেন।
common.read_more